

package jetbrains.buildServer.server.graphql.model.connections;

import graphql.execution.DataFetcherResult;
import graphql.relay.PageInfo;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import jetbrains.buildServer.server.graphql.model.Project;
import jetbrains.buildServer.serverSide.SProject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ProjectsConnection implements ExtensibleConnection<Project, ProjectsConnection.ProjectsConnectionEdge> {
  public static ProjectsConnection empty() {
    return new ProjectsConnection(Collections.emptyList(), PaginationArguments.everything());
  }

  @NotNull
  private final PaginatingConnection<SProject, Project, ProjectsConnectionEdge> myDelegate;

  public ProjectsConnection(@NotNull Collection<SProject> data, @NotNull PaginationArguments paginationArguments) {
    myDelegate = new PaginatingConnection<>(data, ProjectsConnectionEdge::new, paginationArguments);
  }

  public int getCount() {
    return myDelegate.getData().size();
  }

  @NotNull
  @Override
  public DataFetcherResult<List<ProjectsConnectionEdge>> getEdges() {
    return myDelegate.getEdges();
  }

  @Nullable
  @Override
  public PageInfo getPageInfo() {
    return myDelegate.getPageInfo();
  }


  public class ProjectsConnectionEdge extends LazyEdge<SProject, Project> {
    public ProjectsConnectionEdge(@NotNull SProject data) {
      super(data, Project::new);
    }

    @Override
    public String getCursor() {
      return myData.getExternalId();
    }
  }
}